/* global window */
import modelExtend from 'dva-model-extend'
import * as employeesService from './services/employees'
import * as rolesService from '../role/services/roles'
import * as departmentService from '../department/services/departments'
import {pageModel} from 'utils/model'

const {query} = employeesService

export default modelExtend(pageModel, {
  namespace: 'employee',

  state: {
    currentItem: {},
    modalVisible: false,
    resetPasswordModalVisible: false,
    resetRoleModalVisible: false,
    modalType: 'create',
    selectedRowKeys: [],
    selectRecord: {},
    selectedRows: [],
    roles: [],
    departments: [],
    disable: false,
    discoveriesVisible: false,
    qrCode: "",
  },

  subscriptions: {
    setup ({dispatch, history}) {
      history.listen((location) => {
        if (location.pathname === '/employee') {
          const payload = location.query || {current: 1, size: 10}
          dispatch({
            type: 'query',
            payload,
          })
        }
      })
    },
  },

  effects: {

    * query ({payload = {}}, {call, put}) {
      const response = yield call(query, payload)
      const deptResp = yield call(departmentService.query)
      const roleResp = yield call(rolesService.query, {size: 1000})
      if ((roleResp && roleResp.code === 0) || (deptResp && deptResp.code === 0)) {
        yield put({
          type: 'updateState',
          payload: {
            roles: roleResp.data.records,
            departments: deptResp.data,
          },
        })
      }
      const {data} = response
      if (response) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.records,
            pagination: {
              current: Number(payload.current) || 1,
              pageSize: Number(payload.size) || 10,
              total: data.total,
            },
          },
        })
      }
    },

    * delete ({payload}, {call, put, select}) {
      const data = yield call(employeesService.remove, {id: payload})
      const {selectedRowKeys} = yield select(_ => _.employee)
      if (data.code === 0) {
        yield put({type: 'updateState', payload: {selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload)}})
      } else {
        throw data
      }
    },

    * leave ({payload}, {call, put}) {
      const data = yield call(employeesService.leave, payload)
      if (data.code === 0) {
        yield put({type: 'updateState', payload: {selectedRowKeys: []}})
      } else {
        throw data
      }
    },

    * multiDelete ({payload}, {call, put}) {
      const data = yield call(employeesService.batchDelete, payload)
      if (data.code === 0) {
        yield put({type: 'updateState', payload: {selectedRowKeys: []}})
      } else {
        throw data
      }
    },

    * resetPassword ({payload}, {call, put}) {
      const data = yield call(employeesService.resetPassword, payload)
      if (data.code === 0) {
        yield put({
          type: 'updateState', payload: {
            selectedRowKeys: [],
            selectRecord: {},
            resetPasswordModalVisible: false,
          },
        })
      } else {
        throw data
      }
    },

    * create ({payload}, {call, put}) {
      const data = yield call(employeesService.create, payload)
      if (data.code === 0) {
        yield put({type: 'hideModal'})
      } else {
        throw data
      }
    },

    * update ({payload}, {select, call, put}) {
      const id = yield select(({employee}) => {
        if (payload.id) {
          return payload.id
        }
        return employee.currentItem.id
      })

      const newEmployee = {...payload, id}
      const data = yield call(employeesService.update, newEmployee)
      if (data.code === 0) {
        yield put({type: 'hideModal', payload: {
            selectedRowKeys: [],
            selectRecord: {},
            resetRoleModalVisible: false,
          }})
      } else {
        throw data
      }
    },

  },

  reducers: {

    showModal (state, {payload}) {
      return {...state, ...payload, modalVisible: true}
    },

    showResetRoleModal (state) {
      return {...state, resetRoleModalVisible: true}
    },

    showResetPasswordModal (state) {
      return {...state, resetPasswordModalVisible: true}
    },

    hideModal (state) {
      return {...state, modalVisible: false}
    },

    hideResetRoleModal (state) {
      return {...state, resetRoleModalVisible: false}
    },

    hideResetPasswordModal (state) {
      return {...state, resetPasswordModalVisible: false}
    },

    showDiscoveries (state, {payload}) {
      return {...state, ...payload, discoveriesVisible: true}
    },


    hideDiscoveries (state) {
      return {...state, discoveriesVisible: false}
    },

  },
})
